<?php

/**
 * OSECORE
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the GNU General Public License version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@osecore.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade OSECORE to newer
 * versions in the future. If you wish to customize OSECORE for your
 * needs please refer to http://www.osecore.com for more information.
 *
 * @copyright	Copyright (C) 2011 by Sang Le Tuan (http://www.osecore.com). All rights reserved.
 * @license	http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License version 3
 * @author      Sang Le Tuan - batsatla@gmail.com
 */

/**
 * OSECORE Object
 *
 * @package OSECORE_Object
 * @author  Le Tuan Sang <batsatla@gmail.com>
 */
class OSCObject {
	/**
	 *
	 * @var array
	 */
	protected $_dataModifiedKeys = array();
    /**     
     *
     * @var array
     */
    protected $_data;
    /**
     * Original data that was loaded
     *
     * @var array
     */
    protected $_origData;
    /**
     *
     * @var array
     */
    protected $_variableAccessPrefixes = array('', '_');

    /**
     *
     * @param boolean $getObjectVariables
     * @return array
     */
    public function toArray($getObjectVariables = false) {
        return $this->__toArray($getObjectVariables);
    }

    public function __toArray($getObjectVariables = false) {
        if (!$getObjectVariables && isset($this->_data)) {
            return $this->_data;
        }

        return get_object_vars($this);
    }

    /**
     * Convert object to string
     *
     * @return string
     */
    public function __toString() {

    }

    public function __get($name) {

    }

    public function __set($name, $value) {

    }

    /**
     * Property and method call overloading
     *
     * @param string $name
     * @param array $arguments
     * @return mixed
     */
    public function __call($name, $arguments) {
        if (preg_match('/^(get|set)([A-Z])(.*)$/', $name, $matches)) {
            $action = $matches[1];
            $variable = strtolower($matches[2]) . $matches[3];

            $origData = false;

            if ($action == 'get') {
                if (count($arguments) == 1 && $arguments[0]) {
                    $origData = true;
                }
            } else {
                if (count($arguments) == 2 && $arguments[1]) {
                    $origData = true;
                }
            }

            if ($origData ? isset($this->_origData) && is_array($this->_origData) : isset($this->_data) && is_array($this->_data) ) {
                $_variable = preg_replace('/([A-Z])/e', '"_" . strtolower("\\1")', $variable);

                if ($action == 'set') {
                    if ($arguments[0] === null || $arguments[0] === false) {
                        if ($origData) {
                            unset($this->_origData[$_variable]);
                        } else {
                            unset($this->_data[$_variable]);
                        }
                    } else {
                        if ($origData) {
                            $this->_origData[$_variable] = $arguments[0];
                        } else {
                            $this->_data[$_variable] = $arguments[0];
							$this->_dataModifiedKeys[$_variable] = 1;
                        }
                    }

                    return $this;
                } else {
                    if ($origData) {
                        if (isset($this->_origData[$_variable])) {
                            return $this->_origData[$_variable];
                        }
                    } elseif (isset($this->_data[$_variable])) {
                        return $this->_data[$_variable];
                    }
                }
            }

            if (strtolower($variable) != 'variableaccessprefixes') {

                foreach ($this->_variableAccessPrefixes as $prefix) {
                    $_variable = $prefix . $variable;

                    if (isset($this->$_variable)) {
                        if ($action == 'get') {
                            return $this->$_variable;
                        } else {
                            if (count($arguments) == 1) {
                                $this->$_variable = $arguments[0];
                            }

                            return $this;
                        }
                    }
                }
            }

            return $action == 'set' ? $this : false;
        }

        return false;
    }
	
	/**
	 *
	 * @return OSCObject 
	 */
	
	public  function resetDataModifiedKeys() {
		$this->_dataModifiedKeys = array();
		return $this;
	}

}

?>
